// pages/navigation/old/grade/grade.js
const app = getApp()
const select = {
  '学年': ['大一', '大二', '大三', '大四'],
  '学期': ['上', '下']
};

Page({

  /**
   * 页面的初始数据
   */
  data: {
    columns: [{
      values: select['学年'],
      defaultIndex: 0
    },
    {
      values: select['学期'],
      defaultIndex: 0
    }
    ],
    year: '大一',
    term: '上',
    // gradeList: [],
    gpa:''
  },

   /**
   * 发起查询
   */
  onConfirm(e) {
    wx.showLoading({
      title: '正在查询',
    })
    var time = this.data.year+this.data.term;
    wx.cloud.database().collection('score_info')
    .where({semester:time})
    .get()
    .then(res => {
      console.log('查找该学期绩点成功',res.data);
      this.caculator(res.data);
      this.setData({
        gradeList:res.data
      })
      wx.hideLoading();
    })
    .catch(err => {
      console.log('查找该学期绩点失败',err);
    })
  },
  onChange:function(e){
    this.setData({
      year:e.detail.value[0],
      term:e.detail.value[1]
    })
  },
  caculator:function(e){
    console.log("res.data",e);
    var list=e;
    console.log("list",list);
    var score = 0;
    // var gradeList=[];
    for(var i=0;i<list.length;i++){
      score += (list[i].score-50)/10.0;
      // score=score.toFixed(2);
      // console.log("grade",score);
      // list[i].score=score;
      // gradeList.push(list[i]);
    }
    var gpa = (score/(list.length)).toFixed(2)
    this.setData({
      gpa:gpa
    })
    // this.setData({
    //   gradeList:gradeList
    //   })
  },
  onLoad: function (options) {

  }
})